home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 22 / CU Amiga Magazine's Super CD-ROM 22 (1998)(EMAP Images)(GB)[!][issue 1998-05].iso / PowerPC / System / PPCReleaseDEV / Examples / Time.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-21  |  2.6 KB  |  100 lines

  1. #include <exec/memory.h>
  2. #include <proto/timer.h>
  3. #include <proto/exec.h>
  4. #include <stdio.h>
  5. #include "time.h"
  6.  
  7. struct TimerObject
  8. {
  9.     struct Library        *TimerBase;
  10.     struct MsgPort        *MsgPort;
  11.     struct timerequest    *IO;
  12.     struct EClockVal    MyOldEClockVal;
  13.     struct EClockVal    MyEClockVal;
  14.     ULONG            ticks;
  15. };
  16.  
  17. extern struct Library    *SysBase;
  18.  
  19. #define    TimerBase    MyTimerObject->TimerBase
  20.  
  21. void*    __regargs    TimerCreateObject(void)
  22. {
  23. struct TimerObject    *MyTimerObject;
  24.  
  25.   if (MyTimerObject=(struct TimerObject*) AllocVec(sizeof(struct TimerObject),MEMF_ANY))
  26.   {
  27.     if ((MyTimerObject->MsgPort=CreatePort(NULL, 0)) != NULL)
  28.     {
  29.       if ((MyTimerObject->IO=(struct timerequest*) CreateStdIO(MyTimerObject->MsgPort)) != NULL)
  30.       {
  31.         if (OpenDevice("timer.device", UNIT_VBLANK, (struct IORequest *)MyTimerObject->IO, 0) == 0L)
  32.         {
  33.           TimerBase    =(struct Library*) MyTimerObject->IO->tr_node.io_Device;
  34.           return((void*) MyTimerObject);
  35.         }
  36.         DeleteStdIO((struct IOStdReq*) MyTimerObject->IO);
  37.       }
  38.       DeletePort(MyTimerObject->MsgPort);
  39.     }
  40.     FreeVec(MyTimerObject);
  41.   }
  42.   return(NULL);
  43. }
  44.  
  45. void    __regargs    TimerDeleteObject(struct TimerObject    *MyTimerObject)
  46. {
  47.   if (MyTimerObject)
  48.   {
  49.     CloseDevice((struct IORequest*) MyTimerObject->IO);
  50.     DeleteStdIO((struct IOStdReq*) MyTimerObject->IO);
  51.     DeletePort(MyTimerObject->MsgPort);
  52.     FreeVec(MyTimerObject);
  53.   }
  54. }
  55.  
  56. void    __regargs    TimerShow(struct TimerObject    *MyTimerObject)
  57. {
  58. ULONG            allticks,secs,mics;
  59. double            micros;
  60.  
  61.   if (MyTimerObject->MyEClockVal.ev_hi <= MyTimerObject->MyOldEClockVal.ev_hi)
  62.   {
  63.     allticks    =    MyTimerObject->MyEClockVal.ev_lo - MyTimerObject->MyOldEClockVal.ev_lo;
  64.   }
  65.   else
  66.   {
  67.     if (MyTimerObject->MyEClockVal.ev_lo <= MyTimerObject->MyOldEClockVal.ev_lo)
  68.     {
  69.       allticks    =    MyTimerObject->MyEClockVal.ev_lo - MyTimerObject->MyOldEClockVal.ev_lo;
  70.     }
  71.     else
  72.     {
  73.       allticks    =    MyTimerObject->MyOldEClockVal.ev_lo - MyTimerObject->MyEClockVal.ev_lo;
  74.     }
  75.     allticks    +=    (MyTimerObject->MyEClockVal.ev_hi - MyTimerObject->MyOldEClockVal.ev_hi)<<16;
  76.   }
  77.  
  78.   secs        =    allticks / MyTimerObject->ticks;
  79.   micros    =    ((double) 1 / ((double) MyTimerObject->ticks/ (double) ((allticks-(secs*MyTimerObject->ticks)))))*1000000;
  80.   mics        =    micros;
  81.  
  82.   printf("%ld.%06ld secs\n",secs,mics);
  83. }
  84.  
  85. BOOL    __regargs    TimerSetAttr(struct TimerObject    *MyTimerObject,
  86.                                      ULONG        Attr)
  87. {
  88.   if (Attr==TIMERTAG_START)
  89.   {
  90.     MyTimerObject->ticks        =    ReadEClock(&MyTimerObject->MyOldEClockVal);
  91.   }
  92.   else if (Attr==TIMERTAG_STOP)
  93.   {
  94.     MyTimerObject->ticks        =    ReadEClock(&MyTimerObject->MyEClockVal);
  95.   }
  96.  
  97.   return(TRUE);
  98. }
  99.  
  100.